import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';
import {baseDataStatus} from '../Enum';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {injectIntl} from './intl';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
        };
    }

    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const {pageIndex, pageSize, total} = this.props;
        const {formatMessage} = this.props.intl;
        const paginationInfo = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };

        const columns = [
            {
                title: formatMessage({
                    id: 'tablePanel.marketDepartmentName',
                    defaultMessage: '大区'
                }),
                dataIndex: 'marketDepartmentName',
            }, {
                title: formatMessage({
                    id: 'tablePanel.country',
                    defaultMessage: '国家'
                }),
                dataIndex: 'country',
            }, {
                title: formatMessage({
                    id: 'tablePanel.dealerCode',
                    defaultMessage: '总代编号'
                }),
                dataIndex: 'dealerCode',
            }, {
                title: formatMessage({
                    id: 'tablePanel.dealerName',
                    defaultMessage: '总代名称'
                }),
                dataIndex: 'dealerName',
            }, {
                title: formatMessage({
                    id: 'tablePanel.partCode',
                    defaultMessage: '备件编号（奇瑞）'
                }),
                dataIndex: 'partCode',
            }, {
                title: formatMessage({
                    id: 'tablePanel.partName',
                    defaultMessage: '备件名称（奇瑞）'
                }),
                dataIndex: 'partName',
            }, {
                title: formatMessage({
                    id: 'tablePanel.partCodeLocal',
                    defaultMessage: '备件编号（本地）'
                }),
                dataIndex: 'partCodeLocal',
            }, {
                title: formatMessage({
                    id: 'tablePanel.partNameLocal',
                    defaultMessage: '备件名称（本地）'
                }),
                dataIndex: 'partNameLocal',
            }, {
                title: formatMessage({
                    id: 'tablePanel.year',
                    defaultMessage: '年份'
                }),
                dataIndex: 'year',
            }, {
                title: formatMessage({
                    id: 'tablePanel.month',
                    defaultMessage: '月份'
                }),
                dataIndex: 'month',
            }, {
                title: formatMessage({
                    id: 'tablePanel.partInQuantity',
                    defaultMessage: '月入库数'
                }),
                dataIndex: 'partInQuantity',
            }, {
                title: formatMessage({
                    id: 'tablePanel.partOutQuantity',
                    defaultMessage: '月出库数'
                }),
                dataIndex: 'partOutQuantity',
            }, {
                title: formatMessage({
                    id: 'tablePanel.stockQuantity',
                    defaultMessage: '库存数量'
                }),
                dataIndex: 'stockQuantity',
            }, {
                title: formatMessage({
                    id: 'tablePanel.sluggishQuantity',
                    defaultMessage: '呆滞库存'
                }),
                dataIndex: 'sluggishQuantity',
            }, {
                title: formatMessage({
                    id: 'tablePanel.purchaseCurrency',
                    defaultMessage: '采购币种'
                }),
                dataIndex: 'purchaseCurrency',
            }, {
                title: formatMessage({
                    id: 'tablePanel.purchasePrice',
                    defaultMessage: '采购价'
                }),
                dataIndex: 'purchasePrice',
                render: text => formatAmount(text)
            }, {
                title: formatMessage({
                    id: 'tablePanel.saleCurrency',
                    defaultMessage: '销售币种'
                }),
                dataIndex: 'saleCurrency',
            }, {
                title: formatMessage({
                    id: 'tablePanel.salePrice',
                    defaultMessage: '销售价'
                }),
                dataIndex: 'salePrice',
                render: text => formatAmount(text)
            }, {
                title: formatMessage({
                    id: 'tablePanel.updateTime',
                    defaultMessage: '上传时间'
                }),
                dataIndex: 'updateTime',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            }, {
                title: formatMessage({
                    id: 'tablePanel.saveTime',
                    defaultMessage: '接收时间'
                }),
                dataIndex: 'saveTime',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            }, {
                title: formatMessage({
                    id: 'tablePanel.column.status',
                    defaultMessage: '状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(baseDataStatus, text)
            }, {
                title: formatMessage({
                    id: 'tablePanel.column.isCheryPart',
                    defaultMessage: '是否奇瑞件'
                }),
                dataIndex: 'isCheryPart',
                render: text => text ? formatMessage({
                    id: 'tablePanel.column.isCheryPartYes',
                    defaultMessage: '是'
                }) : formatMessage({
                    id: 'tablePanel.column.isCheryPartNo',
                    defaultMessage: '否'
                })
            }
        ];
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={paginationInfo}
                    rowKey="id"
                    locale={locale}
                    {...TABLE} >
                </Table>
            </div>
        );
    }
}

TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    permission: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    refreshListData: PropTypes.func,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    tableSearch,
} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    permission: getPermissions(state),
});
const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));

